package com.jt.tenant.action;

import android.os.Handler;
import android.os.Message;

import com.jt.tenant.conn.GetDiagnosticCar;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Administrator on 2016/3/14.
 */
public abstract class DetectionAction {

    public List<String> codes = new ArrayList<>();

    public DetectionAction(final List<GetDiagnosticCar.Detection> info_list, final GetDiagnosticCar.Info info_net){

        for (int i = 0; i < info_net.list.size(); i ++){

            codes.add(info_net.list.get(i).code);

        }

        final Handler handler = new Handler(){

            @Override
            public void handleMessage(Message msg) {

                switch (msg.what){

                    case 1:

                        onDetectionStart((GetDiagnosticCar.Detection) msg.obj);

                        break;

                    case 2:

                        onDetectionEnd((GetDiagnosticCar.Detection) msg.obj);

                        break;

                    case 3:

                        onEnd((String)msg.obj);

                        break;

                }

            }

        };

        new Thread(){

            @Override
            public void run() {

                Random random = new Random();

                NumberFormat numberFormat = NumberFormat.getInstance();

                int total = 0, totalAuto = 0;

                int[] time = new int[info_list.size()];

                for (int i = 0 ; i < time.length ; i ++){

                    total += time[i] = random.nextInt(1) + 1;

                }

                Message message  = handler.obtainMessage();

                message.what = 0;

                message.sendToTarget();

                for (int i = 0 ; i < info_list.size() ; i ++){

                    try {

                        GetDiagnosticCar.Detection detection = info_list.get(i);

                        detection.isType = !codes.contains(detection.code);

                        message = handler.obtainMessage();

                        message.what = 1;

                        message.obj = detection;

                        message.sendToTarget();

                        Thread.sleep(time[i] * 10);

                        detection.percent = (int) Math.abs((float) (totalAuto += time[i]) / (float) total * 100);

                        message  = handler.obtainMessage();

                        message.what = 2;

                        message.obj = detection;

                        message.sendToTarget();

                    } catch (Exception e) {}

                }

                message  = handler.obtainMessage();

                message.obj = info_net.score;

                message.what = 3;

                message.sendToTarget();

            }

        }.start();

    }

    protected abstract void onDetectionStart(GetDiagnosticCar.Detection detection);

    protected abstract void onDetectionEnd(GetDiagnosticCar.Detection detection);

    protected abstract void onEnd(String score);

}
